﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Common;

namespace OpenWeb.Dal.Infrastructure
{
  public abstract class LoggerBase
  {
    #region Member Variables

    /// <summary>
    /// Member variable to hold the <see cref="ILog"/> instance.
    /// </summary>
    //private readonly log4net.ILog logger = null;

    private LogWriter _logger = null;

    #endregion

    #region Properties

    /// <summary>
    /// Abstract property which must be overridden by the derived classes.
    /// The logger prefix is used to create the logger instance.
    /// </summary>
    protected abstract System.Type LogPrefix
    {
      get;
    }

    #endregion

    #region Constructors

    private static bool isConfigured = false;
    /// <summary>
    /// Constructor of the class.
    /// </summary>
    public LoggerBase()
    {
      _logger = new LogWriterFactory().Create();      
    }

    #endregion

    #region Methods

    #region Protected Methods

    private bool IsNotLogEnabled()
    {
       return  (!this._logger.IsLoggingEnabled());
    }

    /// <summary>
    /// Information level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged.</param>
    protected void LogInfo(string message)
    {
      if (IsNotLogEnabled()) return;      
      this._logger.Write(message, "Info");      
    }

    /// <summary>
    /// Information level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged.</param>
    /// <param name="e">The exception that needs to be logged.</param>
    protected void LogInfo(string message, Exception e)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message + "\r\n" + (e == null ? "" : e.Message), "Info");  
    }

    /// <summary>
    /// Warning level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged.</param>
    protected void LogWarn(string message)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message, "Warn");  
    }

    /// <summary>
    /// Warning level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged.</param>
    /// <param name="e">The exception that needs to be logged.</param>
    protected void LogWarn(string message, Exception e)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message + "\r\n" + (e == null ? "" : e.Message), "Info");  
    }

    /// <summary>
    /// Error level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged.</param>
    protected void LogError(string message)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message, "Error");
    }

    /// <summary>
    /// Error level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged.</param>
    /// <param name="e">The exception that needs to be logged.</param>
    protected void LogError(string message, Exception e)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message + "\r\n" + (e == null ? "" : e.Message), "Error");  
    }

    /// <summary>
    /// Debug level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged</param>
    protected void LogDebug(string message)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message, "Debug");
    }

    /// <summary>
    /// Debug level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged</param>
    /// <param name="e">The exception that needs to be logged</param>
    protected void LogDebug(string message, Exception e)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message + "\r\n" + (e == null ? "" : e.Message), "Debug"); 
    }

    /// <summary>
    /// Fatal level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged</param>
    protected void LogFatal(string message)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message, "Fatal");
    }

    /// <summary>
    /// Fatal level messages are logged to the logger.
    /// </summary>
    /// <param name="message">String that needs to be logged</param>
    /// <param name="e">The exception that needs to be logged</param>
    protected void LogFatal(string message, Exception e)
    {
      if (IsNotLogEnabled()) return;
      this._logger.Write(message + "\r\n" +  (e == null ? "":  e.Message) , "Fatal"); 
    }

    #endregion

    #endregion
  }
}
